package com.sharemarking.wa.platform.payconfig.zfb;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dxy
 */
@Component
public class ZFBPayMain {
    @Value("${ali.pay.appId}")
    private String appId;
    @Value("${ali.pay.privateKey}")
    private String privateKey;
    @Value("${ali.pay.certPath}")
    private String certPath;
    @Value("${ali.pay.alipayPublicCertPath}")
    private String aliPayPublicCertPath;
    @Value("${ali.pay.rootCertPath}")
    private String rootCertPath;
    @Value("${ali.pay.notifyUrl}")
    private String notifyUrl;

    //构造client
    private CertAlipayRequest certAlipayRequest;
    private AlipayClient alipayClient;

    public ZFBPayMain() {
        certAlipayRequest = new CertAlipayRequest();
    }

    @PostConstruct
    private void init() throws AlipayApiException {
        //设置网关地址
        certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
        //设置应用Id
        certAlipayRequest.setAppId(appId);
        //设置应用私钥
        certAlipayRequest.setPrivateKey(privateKey);
        //设置请求格式，固定值json
        certAlipayRequest.setFormat("json");
        //设置字符集
        certAlipayRequest.setCharset("UTF-8");
        //设置签名类型
        certAlipayRequest.setSignType("RSA2");


        //设置应用公钥证书路径
        certAlipayRequest.setCertPath(certPath);
        //设置支付宝公钥证书路径
        certAlipayRequest.setAlipayPublicCertPath(aliPayPublicCertPath);
        //设置支付宝根证书路径
        certAlipayRequest.setRootCertPath(rootCertPath);

        alipayClient = new DefaultAlipayClient(certAlipayRequest);
    }

    public Object AppPay(Map<String, String> map) throws AlipayApiException, FileNotFoundException {
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody(map.get("body"));
        model.setSubject(map.get("subject"));
        model.setOutTradeNo(map.get("code"));
        model.setTimeoutExpress("30m");
        model.setTotalAmount(map.get("transportFees"));
        model.setTimeoutExpress("2m");//该笔订单允许的最晚付款时间，逾期将关闭交易 两分钟后
        model.setProductCode("QUICK_MSECURITY_PAY");
        model.setPassbackParams(map.get("passback_params"));
        request.setBizModel(model);
        request.setNotifyUrl(notifyUrl);

        AlipayTradeAppPayResponse response = null;
        try {
            //这里和普通的接口调用不同，使用的是sdkExecute
            response = alipayClient.sdkExecute(request);
            System.out.println(response.getBody());//就是orderString 可以直接给客户端请求，无需再做处理。
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return response.getBody();
    }


    public String PCPay(Map<String, String> map) throws AlipayApiException, FileNotFoundException {
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();

        //将map内容转为json格式
        request.setBizContent(JSONObject.toJSONString(map));
        request.setNotifyUrl(notifyUrl);

        AlipayTradePagePayResponse response = null;
        try {
            response = alipayClient.pageExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return response.getBody();
    }

    public AlipayTradeQueryResponse query(Map<String, String> map) throws AlipayApiException {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent(JSONObject.toJSONString(map));

        AlipayTradeQueryResponse response = alipayClient.certificateExecute(request);

//        if (response.isSuccess()){
//            System.out.println(response.getCode());
//            System.out.println(response.getMsg());
//            System.out.println(response.getSubCode());
//            System.out.println(response.getSubMsg());
//        }else {
//            System.out.println("11111111111111");
//        }
//        if (response.isSuccess()){
//            return true;
//        }
//        return false;
        return response;
    }

}
